package aeolus.wind.spider.web.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import aeolus.wind.spider.biz.constants.LoginItem;
import aeolus.wind.spider.biz.dataobject.AbilityPackage;
import aeolus.wind.spider.biz.dataobject.Identity;
import aeolus.wind.spider.web.commons.BaseController;

@Controller
public class UserController extends BaseController {

    private static final LoginItem NOT_LOGIN_ITEM = new LoginItem();

    @RequestMapping(value = "manageUser.htm")
    public String manageUser(ModelMap model, boolean isAsUser) {
        if (isAsUser) {
            // 展示自己
            if (checkLogin()) {
                model.addAttribute("identity", getLoginIdentity());
            }
        } else {
            // 展示自己的团队
            if (checkCanLeadGroup()) {
                model.addAttribute("identities", identityService.listIdentitiesLeaded(getLoginName()));
            }
        }
        return getScreen("manageUser");
    }

    @RequestMapping(value = "saveUser.htm")
    public String saveUser(String name, String passwd, boolean isAbleToLeadGroup, ModelMap model, boolean isAsUser) {
        if (checkCanLeadGroup()) {
            if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(passwd)) {
                Identity identity = identityService.findIdentity(name);
                if (identity == null || checkIsMyLeader(identity.getLeaderName())) {
                    if (identity == null) identity = new Identity();
                    identity.setName(name);
                    identity.setPasswd(passwd);
                    identity.setLeaderName(getLoginName());
                    identity.setAbleToLeadGroup(isAbleToLeadGroup);
                    identityService.saveIdentity(identity);
                    // 创建默认的两个能力包
                    if (isAbleToLeadGroup == true) {
                        AbilityPackage abilityPackageSoft = new AbilityPackage();
                        abilityPackageSoft.setOwnIdentityName(name);
                        abilityPackageSoft.setPackageName("软实力");
                        abilityService.saveAbilityPackage(abilityPackageSoft);
                        AbilityPackage abilityPackageHard = new AbilityPackage();
                        abilityPackageHard.setOwnIdentityName(name);
                        abilityPackageHard.setPackageName("硬实力");
                        abilityService.saveAbilityPackage(abilityPackageHard);
                    }
                    model.addAttribute("showMessage", "添加成员" + identity.getName() + "成功");
                }
            }
        }
        return manageUser(model, false);
    }

    @RequestMapping(value = "changePasswd.htm")
    public String changePasswd(String name, String passwd, boolean isAbleToLeadGroup, ModelMap model) {
        if (checkMyself(name)) {
            Identity identity = getLoginIdentity();
            if (StringUtils.isNotBlank(passwd)) {
                identity.setPasswd(passwd);
            }
            identityService.saveIdentity(identity);
            model.addAttribute("showMessage", "修改用户" + identity.getName() + "的密码成功");
        }
        return manageUser(model, true);
    }

    @RequestMapping(value = "deleteUser.htm")
    public String deleteUser(String deleteUserName, ModelMap model) {
        if (checkLogin()) {
            if (StringUtils.isNotBlank(deleteUserName)) {
                Identity identity = identityService.findIdentity(deleteUserName);
                if (identity != null && checkIsMyLeader(identity.getLeaderName())) {
                    identityService.deleteIdentity(deleteUserName);
                    model.addAttribute("showMessage", "删除用户" + identity.getName() + "成功");
                }
            }
        }
        return manageUser(model, false);
    }

    @RequestMapping(value = "login.htm")
    public String login(String name, String passwd, ModelMap model) {
        if (!checkLogin()) {
            LoginItem loginItem = getLoginItem(name, passwd);
            model.addAttribute("loginItem", loginItem);
            if (!loginItem.isLogin()) {
                model.addAttribute("name", name);
                model.addAttribute("passwd", passwd);
            }
        }
        return getScreen("login");
    }

    @RequestMapping(value = "logout.htm")
    public ModelAndView logout(ModelMap model) {
        model.addAttribute("loginItem", NOT_LOGIN_ITEM);

        return new ModelAndView(new RedirectView("login.htm"));
    }

    private LoginItem getLoginItem(String name, String passwd) {
        if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(passwd)) {
            LoginItem loginItem = identityService.findLoginItem(name, passwd);
            if (loginItem != null) return loginItem;
        }
        return NOT_LOGIN_ITEM;
    }
}
